<?php

require_once './library/Zend/Db/Table/Abstract.php';

class TableConfiguration extends Zend_Db_Table
{

    protected $_name;
    protected $_assName;
    protected $_primary;
    protected $_db;
    protected $_registery;
    private $_select;

    protected function _setup()
    {
        $this->_name = 'configcomposants';
        $this->_assName = 'ass_compoconf';
        $this->_primary = 'IdConfiguration';
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');
        parent::_setup();
    }

    public function getConfig($pIdComposant)
    {
        $this->_select = $this->_db->select();
        $this->_select->from(array('conf' => $this->_name));
        $this->_select->join(array('ass' => $this->_assName), 'conf.IdConfiguration = ass.IdConfiguration', array());
        $this->_select->where('ass.IdComposant = ?', $pIdComposant);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        return $result;
    }

    public function getConfigAll()
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        return false;
    }

    public function getConfigId($pIdIdConfiguration)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('IdConfiguration = ?', $pIdIdConfiguration);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        return $result;
    }

    public function getConfigLastId($pIdIdConfiguration)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_assName);
        $this->_select->where('IdConfiguration = ?', $pIdIdConfiguration);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();

        $result = $this->getConfig($result[0]['IdComposant']);
        return end($result);
    }

    public function addConfig($pIdComposant, $pConfiguration)
    {
        $val = array('Date' => Date('d-m-y H:i'),
            'Configuration' => $pConfiguration);
        $this->_db->insert($this->_name, $val);

        $idConf = $this->_db->lastInsertId($this->_name);
        $assVal = array($this->_primary => $idConf,
            'IdComposant' => $pIdComposant);
        $this->_db->insert($this->_assName, $assVal);
    }

    public function deleteConfig($pIdConfig)
    {
        $this->delete($this->_db->quoteInto('IdConfiguration = ?', $pIdConfig));
        $this->_name = $this->_assName;
        $c1 = $this->_db->quoteInto('IdConfiguration = ?', $pIdConfig);
        $c2 = $this->_db->quoteInto('IdComposant = ?', $pIdComposant);
        $this->delete($c1, $c2);
        $this->_name = 'configcomposants';
    }

}
?>